package servlet;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Employee;
import model.JobRes;

import org.json.JSONArray;
import org.json.JSONObject;

import dao.ProfileDAO;

/**
 * Servlet implementation class tabledata
 */
public class jobResData extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProfileDAO pDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public jobResData() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		ArrayList<JobRes> list = new ArrayList<JobRes>();
		Writer out = response.getWriter();
		String jsonString = "";
		
		/**
		 * cast data from database into json to present on Jqgrid in view
		 */
		
		try {
			if(!session.isNew()) {
				pDao = new ProfileDAO();
				Employee em = (Employee) session.getAttribute("person");
				list = pDao.listJobResOfAEmployee(em.getUserid());
				if(!list.isEmpty()) {
					int recNo = list.size();
					
					JSONArray cellArray = new JSONArray();
					
					for(JobRes job : list) {
						JSONObject cellObj = new JSONObject();
						cellObj.put("id", job.getJobResId());
						cellObj.put("goal", job.getGoal());
						cellObj.put("action", job.getAction());
						cellObj.put("target", job.getTarget().toString());
						cellObj.put("complete", job.getComplete().toString());
						cellObj.put("comment", job.getComment());
						cellObj.put("status", job.getStatus());
						cellArray.put(cellObj);
					}
					
					jsonString = "{ \"page\": \"1\", \"record\": \"" + recNo + "\", \"rows\": ";
					jsonString += ( cellArray.toString() + " }"); 

					out.write(jsonString);
					out.flush();
				}
			}
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		String my_rdata = "{ \"page\": \"1\",\"records\": \"1\",\"rows\": ";
//		my_rdata += "	 [ " +				
//									"{\"goal\":\"Goal 1 \",\"action\":\"action 1\",\"target\":\"2013-1-1\",\"complete\":\"2013-3-3\",\"comment\":\"com\",\"status\":\"draft\"}" +           
//							"]}" ;
//		
//		System.out.println(my_rdata);
//		out.write(my_rdata);
//		out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("123456");
	}

}
